package com.yao.shop.controller;



import com.yao.shop.common.Page;
import com.yao.shop.common.Result;
import com.yao.shop.enums.ResultEnums;

import com.yao.shop.pojo.SysUser;
import com.yao.shop.pojo.vo.SysUserVo;
import com.yao.shop.pojo.vo.TokenVo;
import com.yao.shop.service.SysUserService;
import com.yao.shop.shiro.LoginUser;
import com.yao.shop.shiro.SysUserRealm;
import com.yao.shop.shiro.UserToken;
import com.yao.shop.utils.ShiroUtils;
import com.yao.shop.utils.StringUtils;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

@RestController
@RequestMapping("/sysUser")
@Slf4j
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 保存用户
     * @param sysUser
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Result<?> sava(@RequestBody SysUserVo sysUser){
        //参数校验
        if(StringUtils.isBlank(sysUser.getUsername())){
            return  new Result<>(ResultEnums.PARAMS_NULL,"用户名不能为空");
        }
        if(StringUtils.isBlank(sysUser.getPassword())){
            return new Result<>(ResultEnums.PARAMS_NULL,"密码不能为空");
        }
        sysUserService.save(sysUser);
        return  new Result<>("添加成功");
    }

    /**
     * 修改用户
     * 修改接口一般不提供密码修改功能
     * @param sysUser
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public Result<?> update(@RequestBody SysUserVo sysUser){

        sysUserService.update(sysUser);
        return  new Result<>("修改成功");
    }

    /**
     * 删除接口
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    @RequiresPermissions("sys:user:delete")
    public Result<?> delete(@PathVariable Long id){
        sysUserService.delete(id);
        return  new Result<>("删除成功");
    }

    /**
     * 启用接口
     * @param id
     * @return
     */
    @RequestMapping(value = "/enable/{id}",method = RequestMethod.PUT)
    public Result<?> enable(@PathVariable Long id){
        sysUserService.enable(id);
        return new Result<>("启用成功");
    }

    /**
     * 封禁用户
     * @param id
     * @return
     */
    @RequestMapping(value = "/disable/{id}",method = RequestMethod.PUT)
    public Result<?> disable(@PathVariable Long id){
        sysUserService.disable(id);
        return new Result<>("禁用成功");
    }
    /**
     * 分页查询
     *
     * @param page
     * @return
     */
    @RequestMapping(value = "/getByPage", method = RequestMethod.POST)
    public Result<Page<SysUser>> getByPage(@RequestBody Page<SysUser> page) {
        page = sysUserService.getByPage(page);
        return new Result<>(page);
    }
    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public Result<SysUserVo> get(@PathVariable Long id) {
        SysUserVo sysUser = sysUserService.get(id);
        return new Result<>(sysUser);
    }


    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result<TokenVo> login(@RequestBody SysUser sysUser) {
        //校验用户名密码
        if (sysUser == null || StringUtils.isBlank(sysUser.getUsername()) || StringUtils.isBlank(sysUser.getPassword())) {
            return new Result<>(ResultEnums.LOGIN_PARAM_ERROR);
        }
        //使用shiro进行登录
        Subject subject = SecurityUtils.getSubject();
        AuthenticationToken authenticationToken = new UserToken(sysUser.getUsername(), sysUser.getPassword(),SysUserRealm.class);
        try {
            subject.login(authenticationToken);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(ResultEnums.LOGIN_PARAM_ERROR);
        }
        //登录成功
        Serializable sessionId = subject.getSession().getId();
        //更新登录时间
        sysUserService.updateLoginTime(sysUser.getUsername());
        return new Result<>(new TokenVo(sessionId));
    }

    /**
     * 获取登录信息
     */
    @RequestMapping(value="/info",method = RequestMethod.GET)
    public Result<LoginUser> info(){
        LoginUser loginUser=ShiroUtils.getLoginUser();
        return new Result<>(loginUser);
    }

    /**
     * 退出登录
     */
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public Result<?> logout(){
        SecurityUtils.getSubject().logout();
        return new Result<>("退出成功");
    }
}
